Math
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and more.
- Addition
+
: Adds two operands together. - Subtraction
-
: Subtracts the second operand from the first. - Multiplication
*
: Multiplies two operands. - Division
/
: Divides the first operand by the second. The result is always a float.
The order of operations in Python is the same as in mathematics. The acronym PEMDAS can help you remember the order:
- Parentheses
- Exponents
- Multiplication and Division (from left to right)
- Addition and Subtraction (from left to right)
x, y = 3, 6
print(x + y) # Output: 9
print(x - y) # Output: -3
print(x * y) # Output: 18
print(x / y) # Output: 0.5
- If we divide
y
byx
, the result will be2.0
and not2
. This is because the result of division is always a float in Python. - For the other arithmetic operators, the result will be an integer if both operands are integers. If one of the operands is a float, the result will be a float.
2. Other Operators
- Floor Division
//
: Divides the first operand by the second and rounds down the result to an integer. - Modulus
%
: Returns the remainder of the division of the first operand by the second. - Exponentiation
**
: Raises the first operand to the power of the second operand.
The order of operations for these operators is as follows:
- Exponentiation
**
- Floor Division
//
and Modulus%
x, y = 7, 2
print(x // y) # Output: 3 (7 divided by 2 is 3.5, after rounding down we get 3)
print(x % y) # Output: 1 (7 divided by 2 is 3, with a remainder of 1)
print(x ** y) # Output: 49 (7 raised to the power of 2 is 49, 7*7 = 49)
3. Shorthand Operators
Python has shorthand operators that allow you to perform the same operation in a more concise way. These are called in-place operators.
Example:number = 0
number += 5
print(number) # Output: 5
number -=2
print(number) # Output: 3
number +=number
print(number) # Ouput: 6
+=
is shorthand forcount = count +
-=
is shorthand forcount = count -
*=
is shorthand forcount = count *
/=
is shorthand forcount = count /
%=
is shorthand forcount = count %
//=
is shorthand forcount = count //
**=
is shorthand forcount = count **
4. Logical Operators
1. or
The or
operation returns True
if at least one of the operands is True
. This holds even if we have more than two operands:
a, b, c = False, False, True
print(a or b or c) # Output: True
2. and
The and
operation returns True
if both of the operands is True
. This also holds if we have more than two operands:
a, b, c = True, True, True
print(a and b and c) # Output: True
3. not
The not
operation inverts the value of the operand. If the operand is True
, the result is False
. If the operand is False
, the result is True
.
a = True
b = False
print(not a) # Output: False
print(not b) # Output: True
We can also use the operators in combination. For example, we can negate the result of an AND operation:
a, b = True, False
print(not (a and b)) # Output: True